
local localplayer = ac.player.self

class.skill_button = extends(class.button)
{
    name = '技能按钮',
    init = function (self)
        class.button.init(self)
        self.exiocn = self:add_panel()
        self.overlay = self:child_builder
        {
            type = 'panel',
            normal_image = [[UI\Widgets\Console\Human\CommandButton\human-button-lvls-overlay.blp]],
            x = self.w/2,
            y = self.h/2,
            w = self.w/2,
            h = self.h/2,
            text = {
                type = 'text',
                text = '',
                align = 5,
                justH = 'CENTER',
                justV = 'MIDDLE',
                font_size = font_size or 10,
            }
        }
        self.cool = self:child_builder {
            parent = self,
            type = 'text',
            font_size = math.floor(self.h/3),
            align = 5,
            color = 0xffffcc00,
            shadow_color = 0xff000000,
            shadow_offset_x = 02,
            shadow_offset_y = -2,
        }

        self._effect = self:child_builder {
            parent = self,
            type = 'model',
            model = [[UI\Feedback\Autocast\UI-ModalButtonOn.mdl]],
            x = 4,
            y = 2,
            scales = {self.w / 94,self.h / 70},
            show_flag = false,
        }

        return self
    end,
    new = function (parent, image,x, y, w, h,ani_flag)
        local ui = class.skill_button:builder 
        {
            parent = parent,
            normal_image = image,
            x = x,
            y = y,
            w = w,
            h = h,
            ani_flag = ani_flag,
        }
        return ui
    end,

    --刷新显示
    refresh = function(self,skill)
        local unit = ac.unit:localunit()
        self:set_normal_image('')
        self.exiocn:set_normal_image('')
        self.cool:set_text('')
        self.overlay:hide()
        self._effect:hide()
        if not skill then
            return
        end
        local num = skill.show_num
        if skill.cooldown_mode==1 then
            num = skill:get_stack()
        end

        --右下角数值
        if num then
            self.overlay.text:set_text(num)
            self.overlay:show()
        end

        --冷却数值
        local cool = skill:get_cd()
        if cool>0 then
            if cool>1 then
                self.cool:set_text(('%0.f'):format(cool))
            else
                self.cool:set_text(('%0.1f'):format(cool))
            end
        else
            local text = skill.show_cd_num or ''
            self.cool:set_text(text)
        end

        if skill.exiocn then
            self.exiocn:set_normal_image(skill.exiocn)
        else
            self.exiocn:set_normal_image('')
        end
        
        --商店黑图标
        if skill.is_shop_item then
            local buyer = localplayer:get_store_target(unit)
            if not buyer or skill:check_player(localplayer)~=true  then
                self:set_normal_image(skill:get_dis_art())
            end
        end

        --转圈圈动画
        if skill.show_effect and skill:is_enable() then
            self._effect:show()
        end
    end
}
